cmake_minimum_required (VERSION 3.0.0)

project (demo)

if(NOT DEFINED env)
    set(env "wsl")
endif()


file(GLOB MY_SOURCE_DIR 
    ./src/main.c
    ./src/cjson/cjson.c
    ./src/ly/elog/*.c
    ./src/ly/ipc/*.c)

set(MY_INCLUDE_DIR 
    ./inc
    ./inc/cjson
    ./inc/ly/elog
    ./inc/ly/ipc)

# 添加静态库
add_library(libcjson_static STATIC ${CMAKE_CURRENT_SOURCE_DIR}/src/cjson/cjson.c)
set_target_properties(libcjson_static PROPERTIES OUTPUT_NAME "cjson")

# # 添加动态库
add_library(libcjson_shared SHARED ${CMAKE_CURRENT_SOURCE_DIR}/src/cjson/cjson.c)
set_target_properties(libcjson_shared PROPERTIES
    OUTPUT_NAME "cjson"
    VERSION 1.0.0
    SOVERSION 1)

# 设置头文件目录（如果有的话）
target_include_directories(libcjson_static PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/cjson)
target_include_directories(libcjson_shared PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/inc/cjson)

# 安装规则（可选）
# install(TARGETS libcjosn_static mylib_shared DESTINATION lib)
# install(DIRECTORY include/ DESTINATION include)

# 定义一个函数来执行post-build操作
function(add_post_build_copy TARGET_NAME LIBRARY_NAME OUTPUT_DIR)
    add_custom_command(
        TARGET ${TARGET_NAME} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E echo "Building ${TARGET_NAME} is finished."
        COMMAND mkdir -p ${OUTPUT_DIR}
        COMMAND cp ${CMAKE_SOURCE_DIR}/build/${LIBRARY_NAME} ${OUTPUT_DIR}
        COMMENT "Executing post-build commands for ${TARGET_NAME}"
    )
endfunction()

add_post_build_copy(
    libcjson_static "libcjson.a" ${CMAKE_SOURCE_DIR}/lib/cjson/${env})
add_post_build_copy(
    libcjson_shared "libcjson.so" ${CMAKE_SOURCE_DIR}/lib/cjson/${env})

include_directories (${MY_INCLUDE_DIR})


LINK_LIBRARIES(-lm -lpthread -pthread -lpthread)

add_executable(main ${MY_SOURCE_DIR})

message(STATUS "env ${env}")
# target_link_libraries (main ${ZLOG_LIB})
target_link_libraries (main ${CMAKE_SOURCE_DIR}/lib/zlog/${env}/libzlog.so)

set (EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)

add_subdirectory(${PROJECT_SOURCE_DIR}/src/ly/interprocess)
add_subdirectory(${PROJECT_SOURCE_DIR}/src/ly/deamon)